package boot

import (
	"errors"
	"github.com/gogf/gf/util/gconv"
	"github.com/gogf/gf/util/gvalid"
	"web_api_gogf/app/dao"
	_ "web_api_gogf/packed"
)

func init() {
	//注册规则名称唯一性校验
	gvalid.RegisterRule("unique-rule-name", func(rule string, value interface{}, message string, params map[string]interface{}) error {
		var (
			id   = gconv.Int(params["RoleId"])
			name = gconv.String(value)
		)
		n, err := dao.SysRoles.Where("role_id != ? and name = ?", id, name).Count()
		if err != nil {
			return err
		}
		if n > 0 {
			return errors.New(message)
		}
		return nil
	})
}
